/**
 * 修改用户密码功能
 * @author 黄广星
 */

package com.taxsystem.huangguangxing.login;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.CommonOperations;
import com.taxsystem.tool.NetUtil;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AlterPasswordActivity extends Activity {
	
	private static final String ACTION = "com.taxsystem.huangguangxing.login.HTTP_POST_RESPONSE_PASSWORD";
	private String url = "http://itaxmanager.duapp.com/app/user/changepassword";
	
	private LocalBroadcastManager lbm;
	private MyThread myThread;

	private Button alter;
	private EditText oldPassword,newPassword,newSurePassword;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.alter_password_layout);
		
		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(0xff34a4f7));
	
		registerBroadcastReceiver();//注册

		alter = (Button) findViewById(R.id.alter_password);
		oldPassword = (EditText) findViewById(R.id.old_password);
		newPassword = (EditText) findViewById(R.id.new_password);
		newSurePassword= (EditText) findViewById(R.id.new_sure_password);
		
		if(!LoginMassege.isLogin())
		{
			Toast.makeText(AlterPasswordActivity.this, "你还没登录",
					Toast.LENGTH_SHORT).show();
			Intent intent_login = new Intent();
			intent_login.setClass(AlterPasswordActivity.this, LoginActivity.class);
			startActivity(intent_login);
			finish();
		}

		alter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				if (isOK()) {
					if(LoginMassege.isLogin())
					{
						visitNetwork();
					}
					else
					{
						Toast.makeText(AlterPasswordActivity.this, "你还没登录",
								Toast.LENGTH_SHORT).show();
						Intent intent_login = new Intent();
						intent_login.setClass(AlterPasswordActivity.this, LoginActivity.class);
						startActivity(intent_login);
						finish();
					}

//					Toast.makeText(AlterPasswordActivity.this, "修改成功",
//							Toast.LENGTH_SHORT).show();
				}

			}
		});

	}
	
	void visitNetwork() {
		if (CommonOperations.isNetworkConnected(this)) {
			myThread = new MyThread();
			myThread.run();
		} else
			Log.w("tag1", "device can't connect network");
	}

	void registerBroadcastReceiver() {
		lbm = LocalBroadcastManager.getInstance(this);
		IntentFilter filter = new IntentFilter(ACTION);
		BroadcastReceiver myRecive = new MyRecive();
		lbm.registerReceiver(myRecive, filter);
	}
	
	private class MyThread extends Thread {
		@Override
		public void run() {

			Intent intent = new Intent(ACTION);
			try {
				
				 NameValuePair pair1 = new BasicNameValuePair("userId", LoginMassege.getUser().getUserId()+"");
		         NameValuePair pair2 = new BasicNameValuePair("oldPwd",oldPassword.getText().toString());
		         NameValuePair pair3 = new BasicNameValuePair("newPwd",newPassword.getText().toString());
		         
		         List<NameValuePair> pairList = new ArrayList<NameValuePair>();
		         pairList.add(pair1);
		         pairList.add(pair2);
		         pairList.add(pair3);
		         
				String result=NetUtil.getResultPost(url, pairList);
				intent.putExtra("JSON", result);
				lbm.sendBroadcast(intent);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}

	private class MyRecive extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {

			String jsonString = intent.getStringExtra("JSON");
			if (jsonString == null) {

				Log.e("tag1", "MyRecive.onReceive():jsonString is null");
				return;
			}

			try {
				
				JSONObject jsonObject = new JSONObject(jsonString);
			//	String message=jsonObject.getString("message");
				int code=jsonObject.getInt("code");
			//	String serverTime=jsonObject.getString("serverTime");
				if(code==1)
				{
					Toast.makeText(AlterPasswordActivity.this, "密码修改成功",
							Toast.LENGTH_SHORT).show();
					finish();
				}
				else
				{
					Toast.makeText(AlterPasswordActivity.this, "修改失败",
							Toast.LENGTH_SHORT).show();
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {

			}
		}
	}

	// 判断输入信息是否合法
	private boolean isOK() {
		boolean flag = false;
		if (oldPassword.getText().toString().equals(""))
			Toast.makeText(AlterPasswordActivity.this, "旧密码不能为空", Toast.LENGTH_SHORT)
					.show();
		else if (newPassword.getText().toString().equals(""))
			Toast.makeText(AlterPasswordActivity.this, "新密码不能为空", Toast.LENGTH_SHORT)
					.show();
		else if (newSurePassword.getText().toString().equals(""))
			Toast.makeText(AlterPasswordActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT)
					.show();
		else if(!newPassword.getText().toString().equals(newSurePassword.getText().toString()))
			Toast.makeText(AlterPasswordActivity.this, "确认密码不一致", Toast.LENGTH_SHORT)
				.show();
		else
			flag = true;
		return flag;
	}
}
